package com.qianxx.component.activity.recycleview.quick_return;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AbsListView;
import android.widget.Toast;

import com.nineoldandroids.view.ViewPropertyAnimator;
import com.qianxx.component.R;
import com.qianxx.component.activity.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class QuickReturnActivity extends BaseActivity {
    @BindView(R.id.recycler_view)
    RecyclerView mRecyclerView;
    View mHeader;
    int mFlexibleSpaceOffset;
    QuickReturnAdapter adapter;
    // 用来记录当前的头视图是否为显示状态
    boolean mHeaderIsShown;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_quick_return);
        ButterKnife.bind(this);

        mFlexibleSpaceOffset = getResources().getDimensionPixelSize(
                R.dimen.header_height);
        initView();
    }

    private List<String> initData() {
        List<String> list = new ArrayList<>();
        list.add("阿巴斯");
        list.add("奥迪");
        list.add("阿斯顿马丁");
        list.add("宝马");
        list.add("保时捷");
        list.add("奔驰");
        list.add("标致");
        list.add("别克");
        list.add("宾利");
        list.add("比亚迪");
        list.add("长安");
        list.add("大众");
        list.add("福特");
        list.add("海马");
        list.add("皇冠");
        list.add("江淮");
        list.add("捷豹");
        list.add("凯迪拉克");
        list.add("兰博基尼");
        list.add("劳斯莱斯");
        list.add("雷克萨斯");
        list.add("雷诺");
        list.add("力帆");
        list.add("路虎");
        list.add("迈巴赫");
        list.add("马自达");
        list.add("Mini");
        list.add("Nissan");
        list.add("奇瑞");
        list.add("起亚");
        list.add("现代");
        list.add("雪佛兰");
        list.add("雪铁龙");
        list.add("英菲尼迪");
        list.add("中兴");
        return list;
    }

    private void initView() {
        // 头视图
        mHeader = findViewById(R.id.view_header);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this,
                LinearLayoutManager.VERTICAL, false));
        // 创建一个空白的列表首项
        View paddingView = new View(this);
        AbsListView.LayoutParams params = new AbsListView.LayoutParams(
                AbsListView.LayoutParams.MATCH_PARENT, mFlexibleSpaceOffset);
        paddingView.setLayoutParams(params);
        paddingView.setBackgroundColor(Color.WHITE);

        adapter = new QuickReturnAdapter(this, initData());
        // 添加空白的首项,首项要和头视图一样的高度,头视图会覆盖首项
        adapter.addHeaderView(paddingView);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            // 用来记录每次垂直滚动的距离
            int mScrollY;

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView,
                                             int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                // 如果当前状态为闲置时，mScrollY置零,表示不再滚动
                if (newState == RecyclerView.SCROLL_STATE_IDLE) {
                    mScrollY = 0;
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                mScrollY += dy;
                // 如果滚动距离大于12,隐藏头视图,反之显示
                if (mScrollY > 12) {
                    hideHeader();
                } else {
                    showHeader();
                }
            }
        });

        adapter.setOnItemClickListener(new QuickReturnAdapter.OnItemClickListener() {

            @Override
            public void onItemClick(View view, int position) {
                Toast.makeText(getApplicationContext(), position + "",
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void showHeader() {
        if (!mHeaderIsShown) {
            ViewPropertyAnimator.animate(mHeader).cancel();
            ViewPropertyAnimator.animate(mHeader).translationY(0)
                    .setDuration(200).start();
            mHeaderIsShown = true;
        }
    }

    private void hideHeader() {
        if (mHeaderIsShown) {
            ViewPropertyAnimator.animate(mHeader).cancel();
            ViewPropertyAnimator.animate(mHeader)
                    .translationY(-mFlexibleSpaceOffset).setDuration(200)
                    .start();
            mHeaderIsShown = false;
        }
    }

    @Override
    public boolean setEnableTitleBar() {
        return false;
    }
}
